package com.lcredu.dao.impl;

import com.lcredu.dao.StudentInfoDAO;
import com.lcredu.entity.StudentInfo;
import com.lcredu.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentInfoDAOImpl implements StudentInfoDAO {
    @Override
    public boolean add(StudentInfo studentInfo) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        boolean flag = false;
        @SuppressWarnings("all")
        String sql = "INSERT INTO studentInfo(sname, SGENDER, SAGE, SADDRESS, SEMALL) VALUE (?,?,?,?,?);";
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, studentInfo.getSname());
            preparedStatement.setString(2, studentInfo.getSgender());
            preparedStatement.setInt(3, studentInfo.getSage());
            preparedStatement.setString(4, studentInfo.getSaddress());
            preparedStatement.setString(5, studentInfo.getSemail());
            if (preparedStatement.executeUpdate() > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, preparedStatement);
        }
        return flag;
    }

    @Override
    public boolean delete(int sid) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        boolean flag = false;
        @SuppressWarnings("all")
        String sql = "delete from studentInfo where sid = ?";
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, sid);

            if (preparedStatement.executeUpdate() > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, preparedStatement);
        }
        return flag;
    }

    @Override
    public boolean update(StudentInfo studentInfo) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        boolean flag = false;
        @SuppressWarnings("all")
        String sql = " update studentInfo set sname=?,sgender=?,sage=?,saddress=?,semall=? where sid = ?";
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, studentInfo.getSname());
            preparedStatement.setString(2, studentInfo.getSgender());
            preparedStatement.setInt(3, studentInfo.getSage());
            preparedStatement.setString(4, studentInfo.getSaddress());
            preparedStatement.setString(5, studentInfo.getSemail());
            preparedStatement.setInt(5, studentInfo.getSid());
            if (preparedStatement.executeUpdate() > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, preparedStatement);
        }
        return flag;
    }

    @Override
    public List<StudentInfo> getAll() {
        ArrayList<StudentInfo> students = new ArrayList<>();
        String sql = "select * from studentInfo";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                StudentInfo studentInfo = new StudentInfo();
                studentInfo.setSid(resultSet.getInt("sid"));
                studentInfo.setSname(resultSet.getString("sname"));
                studentInfo.setSgender(resultSet.getString("sgender"));
                studentInfo.setSage(resultSet.getInt("sage"));
                studentInfo.setSaddress(resultSet.getString("saddress"));
                studentInfo.setSemail(resultSet.getString("semail"));
                students.add(studentInfo);
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return students;
    }

    @Override
    public StudentInfo getStudentInfoById(int sid) {
        StudentInfo studentInfo = new StudentInfo();
        String sql = "select * from studentInfo where sid = ?";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,sid);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                studentInfo.setSid(resultSet.getInt("sid"));
                studentInfo.setSname(resultSet.getString("sname"));
                studentInfo.setSgender(resultSet.getString("sgender"));
                studentInfo.setSage(resultSet.getInt("sage"));
                studentInfo.setSaddress(resultSet.getString("saddress"));
                studentInfo.setSemail(resultSet.getString("semail"));
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return studentInfo;
    }
}
